﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Commons.DataFilters;

namespace SD.DataAccess
{
    /// <summary>
    /// SupportingMaterialDao Class
    /// </summary>
    [Serializable]
    public class SupportingMaterialDao
    {
        [NonSerialized]
        private readonly ServiceDBDataContext _context = new ServiceDBDataContext();
        /// <summary>
        /// Method GetList
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="sortType"></param>
        /// <param name="startRowIndex"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public IQueryable<SupportingMaterial> GetList(SupportingMaterialFilter filter, string sortType, int startRowIndex, int max) 
        {
            IQueryable<SupportingMaterial> data = from sm in _context.SupportingMaterials
                                                   select sm;
            //filter
            if (filter != null) 
            {
                data = GetListQuery(data, filter);
            }
            //sort data 
            data = GetListSort(data, sortType);
            //Limit the results
            data = data.Skip(startRowIndex).Take(max);
            //return data
            return data;
        }
        /// <summary>
        /// Method GetListQuery
        /// </summary>
        /// <param name="data"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        private static IQueryable<SupportingMaterial> GetListQuery(IQueryable<SupportingMaterial> data, SupportingMaterialFilter filter)
        {
            data = data.Where(sm => sm.OrganizationId == filter.OrganizationId);

            if (!filter.IncludeInactive)
            {
                data = data.Where(sm => sm.IsActive != filter.IncludeInactive);
            }
            return data;
        }
        /// <summary>
        /// Method Get List sort
        /// </summary>
        /// <param name="data"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        private static IQueryable<SupportingMaterial> GetListSort(IQueryable<SupportingMaterial> data, string sortType) 
        {
            bool sortDescending = false;
            if (!string.IsNullOrEmpty(sortType))
            {
                string[] values = sortType.Split(' ');
                sortType = values[0];
                if (values.Length > 1)
                {
                    sortDescending = (values[1] == "DESC");
                }

                switch (sortType)
                {
                    case "URL":
                        data = sortDescending ? data.OrderByDescending(sm => sm.Url) : data.OrderBy(sm => sm.Url);
                        break;
                    case "Description":
                        data = sortDescending
                                   ? data.OrderByDescending(sm => sm.Description)
                                   : data.OrderBy(sm => sm.Description);
                        break;
                    case "AddedBy":
                        data = sortDescending
                                   ? data.OrderByDescending(sm => sm.User.Account)
                                   : data.OrderBy(sm => sm.User.Account);
                        break;
                    case "Type":
                        data = sortDescending ? data.OrderByDescending(sm => sm.Type) : data.OrderBy(sm => sm.Type);
                        break;
                    case "AddedDate":
                        data = sortDescending
                                   ? data.OrderByDescending(sm => sm.AddedDate)
                                   : data.OrderBy(sm => sm.AddedDate);
                        break;
                    case "IsActive":
                        data = sortDescending
                                   ? data.OrderByDescending(sm => sm.IsActive)
                                   : data.OrderBy(sm => sm.IsActive);
                        break;
                }
            }
            return data;
        }
        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public int GetListCount(SupportingMaterialFilter filter)
        {
            IQueryable<SupportingMaterial> data = from sm in _context.SupportingMaterials
                                                  select sm;
            if (filter != null)
            {
                data = GetListQuery(data, filter);
            }
            return data.Count();
        }
        /// <summary>
        /// Method Get supporting material by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SupportingMaterial GetSupportingMaterialById(int id)
        {
            SupportingMaterial supportingMaterial = _context.SupportingMaterials.FirstOrDefault(sm => sm.OrganizationId == id);
            return supportingMaterial;
        }
        /// <summary>
        /// Mthod Select All Type of supporting Material
        /// </summary>
        /// <returns></returns>
        public IQueryable SelectAllTypeSupportingMaterial() 
        {
            var data = (from tsm in _context.SupportingMaterials
                        select new { TypeId = tsm.Type, TypeName = tsm.Type }).Distinct();
            return data;
        }
        /// <summary>
        /// Method insert new supporting material
        /// </summary>
        /// <param name="supportingMaterial"></param>
        /// <returns></returns>
        public bool InsertSupportingMaterial(SupportingMaterial supportingMaterial) 
        {
            _context.SupportingMaterials.InsertOnSubmit(supportingMaterial);
            _context.SubmitChanges();
            return true;
        }
        /// <summary>
        /// Method Edit Supporting Material
        /// </summary>
        /// <param name="supportingMaterial"></param>
        /// <returns></returns>
        public bool EditSupportingMaterial(SupportingMaterial supportingMaterial)
        {
            bool flag = false;
            SupportingMaterial supportingMaterial1 = _context.SupportingMaterials.FirstOrDefault(sm => sm.SupportingMaterialId == supportingMaterial.SupportingMaterialId);
            try
            {
                if (supportingMaterial != null)
                {
                    supportingMaterial1.Url = supportingMaterial.Url;
                    supportingMaterial1.Type = supportingMaterial.Type;
                    supportingMaterial1.Description = supportingMaterial.Description;
                    supportingMaterial1.IsActive = supportingMaterial.IsActive;
                    _context.SubmitChanges();
                    return flag;
                }
            }
            catch (Exception ex) 
            {
                throw new Exception(ex.Message);
            }
            return flag;
        }
        /// <summary>
        /// Method Update status supporting material only
        /// </summary>
        /// <param name="supportingMaterialId"></param>
        /// <param name="status"></param>
        public void UpdateStatusSupportingMaterial(int supportingMaterialId, bool status) 
        {
            SupportingMaterial supportingMaterial = _context.SupportingMaterials.FirstOrDefault(sm => sm.SupportingMaterialId == supportingMaterialId);
            if (supportingMaterial != null) 
            {
                supportingMaterial.IsActive = status;
                _context.SubmitChanges();
            }
        }
    }
}
